package com.advert.boot.system.controller;

import cn.hutool.core.collection.CollUtil;
import com.advert.boot.common.exception.BusinessException;
import com.advert.boot.common.result.PageResult;
import com.advert.boot.common.result.Result;
import com.advert.boot.common.result.ResultCode;
import com.advert.boot.system.model.form.ProductForm;
import com.advert.boot.system.model.query.ProductPageQuery;
import com.advert.boot.system.model.vo.ProductVO;
import com.advert.boot.system.model.vo.StatisticsVO;
import com.advert.boot.system.service.AdvertProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品信息前端控制层
 *
 * @author Theo
 * @since 2024-07-30 11:25
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Tag(name = "15.后台管理-产品信息")
@RequestMapping("/api/admin/product")
public class AdvertProductController {
    private final AdvertProductService productService;

    @Operation(summary = "1首页（统计信息）")
    @GetMapping("/statistics")
    public Result<StatisticsVO> statistics() {
        return Result.success(productService.statistics());
    }

    @Operation(summary = "2产品信息")
    @GetMapping("/page")
    public PageResult<ProductVO> page(@ParameterObject ProductPageQuery pageQuery) {
        return PageResult.success(productService.page(pageQuery, 2));
    }

    @Operation(summary = "3新增")
    @PostMapping
    public Result<?> save(@RequestBody @Valid ProductForm productForm) {
        return Result.judge(productService.save(productForm));
    }

    @Operation(summary = "4修改")
    @PutMapping(value = "/{id}")
    public Result<?> update(@Valid @PathVariable Long id, @RequestBody ProductForm productForm) {
        return Result.judge(productService.edit(id, productForm));
    }

    @Operation(summary = "5删除")
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        return Result.judge(productService.delete(id));
    }

    @Operation(summary = "6批量删除")
    @PostMapping("/delete")
    public Result<?> deletes(@RequestBody List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            throw new BusinessException(ResultCode.FAILED, "请选择数据！");
        }

        ids.forEach(id ->{
            productService.delete(id);
        });
        return Result.success();
    }
}
